# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :example.py
# @Time      :2025/6/12 20:35
# @Author    :aiyimu


# 电商应用场景
async def cluster_ecommerce():
    # 初始化集群客户端
    rc = RedisCluster(startup_nodes=[{"host": "localhost", "port": 6379}])
    # 商品详情缓存（自动分片）
    product_id = 1001
    await rc.hset(
        f"product: {product_id}",
        mapping = {
            "name": "smartphone",
            "price": "2999",
            "stock": "100"
        }
    )
    # 商品排行榜（有序集合）
    await rc.zadd("product:ranking", {"product:A": 150, "product:B": 200})
    # 获取TOP3商品
    top_products = await rc.zrevrange("product:ranking", 0, 2, withscores=True)
    print("热销商品:", top_products)